package com.example.sunsg.okhttptest.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.breadtrip.R;
import com.example.sunsg.okhttptest.customview.CanvasDemo;
import com.example.sunsg.okhttptest.customview.ScaleMoney;
import com.example.sunsg.okhttptest.customview.TestWatchView;

public class AutoActivity extends AppCompatActivity {
    /**更改角度*/
    private static final int TYPE_DEGREE = 1;
    private int mDegree = -90;
    private TestWatchView mTestWatchView;

    //=================================================
    private TextView tvValue;
    private ScaleMoney svView;

    public static void launch(Activity activity){
        Intent intent = new Intent(activity,AutoActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        initView();
        rxJava();
        canvalsDemo();
        handerDegree();
    }

    private void initView(){
        mTestWatchView = (TestWatchView) findViewById(R.id.testWatch);
//        mTestWatchView.setDegree(30);

        tvValue = (TextView) findViewById(R.id.tv_value);
        svView = (ScaleMoney) findViewById(R.id.sm_view);
        svView.setMoveScaleInterface(new ScaleMoney.MoveScaleInterface() {
            @Override
            public void getValue(int value) {
                tvValue.setText(value + "");
            }
        });
    }


    private void rxJava(){

    }

    private void canvalsDemo(){
        final CanvasDemo canvasDemo = (CanvasDemo) findViewById(R.id.canvas_demo);
//        canvasDemo.drawLine();
//          canvasDemo.drawNormal();
//        canvasDemo.drawLineGraph();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
//                canvasDemo.drawLineGraph();
                canvasDemo.drawBitmap();
            }
        },2000);

    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg != null){
                switch (msg.what){
                    case TYPE_DEGREE:
                        mDegree += 6;
                        mTestWatchView.setDegree(mDegree);
//                        Log.i("test","degree = "+mDegree);
                        if(mDegree % 360 == 0){
                            mDegree = 0;
                        }
                        handerDegree();
                        break;

                }
            }
        }
    };

    private void handerDegree(){
        mHandler.sendEmptyMessageDelayed(TYPE_DEGREE,1000);
    }




}
